package com.yike.user.controller;

import com.yike.common.api.Add;
import com.yike.common.api.ApiResult;
import com.yike.common.api.Update;
import com.yike.common.base.BaseController;
import com.yike.common.enums.OperationLogType;
import com.yike.common.log.Module;
import com.yike.common.log.OperationLog;
import com.yike.common.pagination.Paging;
import com.yike.user.param.MerchantPageParam;
import com.yike.user.param.MerchantParam;
import com.yike.user.service.PlatformMerchantService;
import com.yike.user.vo.PlatformMerchantVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * 平台商户 控制器
 *
 * @author Sakura
 * @since 2024-10-31
 */
@Slf4j
@RestController
@RequestMapping("/platform/merchant")
@Module("user")
@Api(value = "平台商户API", tags = {"平台商户管理"})
@ApiIgnore
public class PlatformMerchantController extends BaseController {

    @Autowired
    private PlatformMerchantService platformMerchantService;

    /**
     * 添加商户
     */
    @PostMapping("/add")
    @OperationLog(name = "添加商户", type = OperationLogType.ADD)
    @ApiOperation(value = "添加商户", response = ApiResult.class)
    public ApiResult<Boolean> addMerchant(@Validated(Add.class) @RequestBody MerchantParam merchantParam) throws Exception {
        boolean flag = platformMerchantService.saveMerchant(merchantParam);
        return ApiResult.result(flag);
    }

    /**
     * 修改商户
     */
    @PostMapping("/update")
    @OperationLog(name = "修改商户", type = OperationLogType.UPDATE)
    @ApiOperation(value = "修改商户", response = ApiResult.class)
    public ApiResult<Boolean> updateMerchant(@Validated(Update.class) @RequestBody MerchantParam merchantParam) throws Exception {
        boolean flag = platformMerchantService.updateMerchant(merchantParam);
        return ApiResult.result(flag);
    }

    /**
     * 删除商户
     */
    @PostMapping("/delete/{id}")
    @OperationLog(name = "删除商户", type = OperationLogType.DELETE)
    @ApiOperation(value = "删除商户", response = ApiResult.class)
    public ApiResult<Boolean> deleteMerchant(@PathVariable("id") Long id) throws Exception {
        boolean flag = platformMerchantService.deleteMerchant(id);
        return ApiResult.result(flag);
    }

    @PostMapping("/batchDel")
    @OperationLog(name = "批量删除商户", type = OperationLogType.UPDATE)
    @ApiOperation(value = "批量删除商户", response = ApiResult.class)
    public ApiResult<Boolean> batchDel(@Validated(Update.class) @RequestBody List<Long> ids) throws Exception {
        boolean flag = platformMerchantService.batchDel(ids);
        return ApiResult.result(flag);
    }

    /**
     * 商户详情
     */
    @GetMapping("/info/{id}")
    @OperationLog(name = "商户详情", type = OperationLogType.INFO)
    @ApiOperation(value = "商户详情", response = PlatformMerchantVo.class)
    public ApiResult<PlatformMerchantVo> getMerchant(@PathVariable("id") Long id) throws Exception {
        PlatformMerchantVo platformMerchantVo = platformMerchantService.getMerchant(id);
        return ApiResult.ok(platformMerchantVo);
    }

    @PostMapping("/getPageList")
    //@OperationLog(name = "商户分页列表", type = OperationLogType.PAGE)
    @ApiOperation(value = "商户分页列表", response = PlatformMerchantVo.class)
    public ApiResult<Paging<PlatformMerchantVo>> getMerchantPageList(@Validated @RequestBody MerchantPageParam merchantPageParam) throws Exception {
        Paging<PlatformMerchantVo> paging = platformMerchantService.getMerchantPageList(merchantPageParam);
        return ApiResult.ok(paging);
    }


}

